<%--
  Created by IntelliJ IDEA.
  User: dzw
  Date: 2018/1/4
  Time: 13:08
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%
  String path = request.getContextPath();
  String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
  String _id=request.getParameter("_");
  String jsessionid = session.getId();
%>

<form id="form_leave_input" class="mui-input-group">

  <input type="hidden" name="keyId" id="keyId" value="${leave.id}"/>

  <div class="mui-input-row">
    <label>请假人员</label>
    <input readonly type="text" id="leaveUser" class="mui-input-clear" placeholder="请选择请假人员" value="${leave==null?user.name:leave.leaveUser.name}">
    <input type="hidden" id="leaveUserId" name="leaveUserId" value="${leave==null?user.id:leave.leaveUser.id}" />
  </div>

  <div class="mui-input-row">
    <label>请假类别</label>
    <input readonly type="text" id="leaveType" name="leaveType" class="mui-input-clear" placeholder="请选择请假类别" value="${leave.leaveType.name}">
    <input type="hidden" id="leaveTypeId" name="leaveTypeId" value="${leave.leaveType.id}" />
  </div>

  <div class="mui-input-row">
    <label>请假天数</label>
    <div id="leaveDayDiv" class="mui-numbox" data-numbox-step="1" data-numbox-min="0" >
      <button class="mui-btn mui-btn-numbox-minus" type="button">-</button>
      <input class="mui-input-numbox" type="number" id="leaveDayNum" name="leaveDayNum" value="${leave.leaveDayNum}" >
      <button class="mui-btn mui-btn-numbox-plus" type="button">+</button>
    </div>
  </div>

  <div class="mui-input-row">
    <label>请假开始时间</label>
    <input readonly type="text" id="leaveStartDate" name="leaveStartDate" class="mui-input-clear" placeholder="请选择请假开始时间" value="<fmt:formatDate pattern="yyyy-MM-dd" value="${leave.leaveStartDate}" />">
  </div>

  <div class="mui-input-row">
    <label>请假结束时间</label>
    <input readonly type="text" id="leaveEndDate" name="leaveEndDate" class="mui-input-clear" placeholder="请选择请假结束时间" value="<fmt:formatDate pattern="yyyy-MM-dd" value="${leave.leaveEndDate}" />">
  </div>

  <div class="mui-input-row">
    <label>请假事由</label>
    <input type="text" id="leaveReason" name="leaveReason" class="mui-input-clear" placeholder="请输入请假事由" value="${leave.leaveReason}" >
  </div>

  <h5 class="mui-content-padded">下一步流程</h5>
  <div class="mui-input-row mui-radio">
    <label>归档</label>
    <input type="radio" id="nextNo" name="next" value="0">
  </div>
  <div class="mui-input-row mui-radio">
    <label>选择下一步操作人</label>
    <input type="radio" id="nextYes" name="next" value="1">
  </div>

  <div id="showNextStep" class="mui-input-row" style="display: none">
    <label>下一操作人</label>
    <input readonly type="text" id="editors" name="editors" class="mui-input-clear" placeholder="请选择下一步操作人">
    <input type="hidden" id="editorsId" name="editorsId">
  </div>

</form>

<div class="mui-col-xs-12 mui-col-sm-12">
  <button type="button" class="mui-btn" id="apply-re-common">返回</button>
  <button type="button" class="mui-btn mui-btn-royal" id="btn-save-common">保存</button>
  <c:if test="${leave==null || leave.processState=='Draft'}">
    <button type="button" class="mui-btn mui-btn-success" id="btn-commit-common">提交</button>
  </c:if>
  <c:if test="${leave!=null && leave.processState=='Backed'}">
    <button type="button" class="mui-btn mui-btn-success" id="btn-recommit-common">再提交</button>
  </c:if>
</div>

<script>

  mui("#leaveDayDiv").numbox();
  var leaveType=${leaveType};

  var index = "${index}";
  var todo = "${todo}";
  var remind = "${remind}";
  var record = "${record}";
  var draft =  "${draft}";

  $(function(){
    var pdata= {
      keyId:$("#keyId").val(),
      entityName:"com.fz.us.oa.core.entity.com.leave"
    };
    fn_leave_input_validate()
  });

  //下一步点击
  $(':radio[name="next"]').click(function(){
    if($(this).val() == "1"){
      $('#showNextStep').show();
    }else{
      $('#showNextStep').hide();
    }
  });

  //选择开始时间
  $("#leaveStartDate").click(function(){
    var dtPicker = new mui.DtPicker();
    dtPicker.show(function (sr) {
      $("#leaveStartDate").val(sr.value);
    })
  });

  //选择结束时间
  $("#leaveEndDate").click(function(){
    var dtPicker = new mui.DtPicker();
    dtPicker.show(function (sr) {
      $("#leaveEndDate").val(sr.value);
    })
  });

  //选择下一步审批人
  $("#editors").click(function(){
    __ajax_get("<%=path%>/main/member/list",null,function(data){
      var userArr=data.dataRows;
      var arr=new Array();
      for(var i=0;i<userArr.length;i++){
        var user={"value":userArr[i].id,"text":userArr[i].name};
        arr.push(user);
      }
      var picker = new mui.PopPicker();
      picker.setData(arr);
      picker.show(function (selectItems){
        $("#form_leave_input #editors").val(selectItems[0].text);
        $("#form_leave_input #editorsId").val(selectItems[0].value);
        $("#editors").blur();
      });
    });
  });

  //选择请假人员
  $("#leaveUser").click(function(){
    __ajax_get("<%=path%>/main/member/list",null,function(data){
      var userArr=data.dataRows;
      var arr=new Array();
      for(var i=0;i<userArr.length;i++){
        var user={"value":userArr[i].id,"text":userArr[i].name};
        arr.push(user);
      }
      var picker = new mui.PopPicker();
      picker.setData(arr);
      picker.show(function (selectItems){
        $("#form_leave_input #leaveUser").val(selectItems[0].text);
        $("#form_leave_input #leaveUserId").val(selectItems[0].value);
        $("#editors").blur();
      });
    });
  });

  //选择请假类别
  $("#leaveType").click(function(){
    var picker = new mui.PopPicker();
    picker.setData(leaveType);
    picker.show(function (selectItems){
      $("#form_leave_input #leaveType").val(selectItems[0].text);
      $("#form_leave_input #leaveTypeId").val(selectItems[0].value);
      $("#editors").blur();
    });
  });

  // 表单数据校验
  function fn_leave_input_validate() {
    __init_validate('form_leave_input', {
      rules : {
        leaveUserId : {
          required : true
        },
        leaveTypeId : {
          required : true
        },
        number : {
          required : true,
          min : 0.5
        },
        leaveStartDate : {
          required : true
        },
        leaveEndDate:{
          required : true
        },
        leaveReason:{
          required : true
        },
        next:{
          required:true
        },
        editorsId:{
          required:function(){
            if($(':checked[name="next"]').val() == "1"){
              return true;
            }else{
              return false;
            }
          }
        }
      },
      messages : {
        leaveUserId : {
          required : "请选择请假人员"
        },
        leaveTypeId : {
          required : "请选择请假类别"
        },
        number : {
          required : "请选择请假天数",
          min : "请选择请假天数"
        },
        leaveStartDate : {
          required : "请选择请假开始日期"
        },
        leaveEndDate : {
          required : "请选择请假结束日期"
        },
        leaveReason : {
          required : "请输入请假事由"
        },
        next:{
          required : "请选择下一步流程"
        },
        editorsId : {
          required : "请选择下一步操作人"
        }
      }
    });
  }

  //保存
  $("#btn-save-common").click(function(){
    mui.confirm("确定保存吗","提示",["确定","取消"],function(e){
      if(e.index==0){
        __form_save("form_leave_input", "<%=path%>/mp/leave/save",function(data){
          $("#apply-re-common").trigger("click");
          mui.toast(data.message);
        })
      }
    });
  });

  //提交
  $("#btn-commit-common,#btn-recommit-common").click(function(){
    if (!$('#form_leave_input').valid()) {
      mui.alert("请填写完整");
      return false;
    }
    mui.confirm("确定提交吗","提示",["确定","取消"],function(e){
      if(e.index==0){
        __form_save("form_leave_input", "<%=path%>/mp/leave/commit",function(data){
          $("#apply-re-common").trigger("click");
          mui.toast(data.message);
        })
      }
    });
  });


  //返回视图
  $("#apply-re-common").click(function(){
    if(index==1){
      loadURL("menu/dashboard",$('#page-content'));
    }else if(todo==1){
      loadURL("menu/toDo",$('#page-content'));
    }else if(remind==1){
      loadURL("menu/remind",$('#page-content'));
    }else if(record==1){
      loadURL("menu/taskRecord?type=1",$('#page-content'));
    }else if(record==2){
      loadURL("menu/taskRecord?type=2",$('#page-content'));
    }else if(draft==1){
      loadURL("menu/draft",$('#page-content'));
    }else{
      loadURL("menu/com/leave?viewtype=2",$('#page-content'));
    }
  });

</script>